﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BOSS.Domain.Security;
using BOSS.Dao.Security;

namespace BOSS.Dao.NHibernate.Security
{
    /// <summary>
    /// 角色权限设置 DAO
    /// </summary>
    public class RolePrivilegeSettingDao : EntityDao<RolePrivilegeSetting, int>, IRolePrivilegeSettingDao
    {
        #region IRolePrivilegeSettingDao Members

        /// <summary>
        /// 获取指定角色的权限集合
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public PrivilegeGroup SelectByRole(Role role)
        {
            IList<RolePrivilegeSetting> settings = this.HibernateTemplate.Find<RolePrivilegeSetting>(
                "from RolePrivilegeSetting s where s.Role = ?", role);
            PrivilegeGroup privilegeGroup = new PrivilegeGroup();
            foreach (RolePrivilegeSetting setting in settings)
            {
                privilegeGroup.AddPrivilege(new Privilege(setting.PrivilegeName, setting.Permission));
            }
            return privilegeGroup;
        }

        /// <summary>
        /// 查询指定权限的所有设置
        /// </summary>
        /// <param name="privilegeName"></param>
        /// <returns></returns>
        public IList<RolePrivilegeSetting> SelectByName(string privilegeName)
        {
            return this.HibernateTemplate.Find<RolePrivilegeSetting>(
                "from RolePrivilegeSetting s where s.PrivilegeName = ?", privilegeName);
        }

        /// <summary>
        /// 为指定角色添加权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="privilege"></param>
        /// <returns></returns>
        public int Insert(Role role, Privilege privilege)
        {
            return (int)this.HibernateTemplate.Save(new RolePrivilegeSetting()
            {
                Role = role,
                PrivilegeName = privilege.Name,
                Permission = privilege.Permission
            });
        }

        /// <summary>
        /// 为指定角色更新权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="privilege"></param>
        public void Update(Role role, Privilege privilege)
        {
            RolePrivilegeSetting setting = this.HibernateTemplate.Find<RolePrivilegeSetting>(
                "from RolePrivilegeSetting s where s.Role = ? and s.PrivilegeName = ?", new object[] { role, privilege.Name })
                .FirstOrDefault();
            if (setting != null)
            {
                setting.Permission = privilege.Permission;
                this.HibernateTemplate.Update(setting);
            }
        }

        /// <summary>
        /// 为指定角色移除权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="privilege"></param>
        public void Delete(Role role, Privilege privilege)
        {
            RolePrivilegeSetting setting = this.HibernateTemplate.Find<RolePrivilegeSetting>(
               "from RolePrivilegeSetting s where s.Role = ? and s.PrivilegeName = ?", new object[] { role, privilege.Name })
               .FirstOrDefault();
            if (setting != null)
            {
                this.HibernateTemplate.Delete(setting);
            }
        }

        #endregion
    }
}
